// pages/person/index.js
// import regeneratorRuntime from '../../utils/regenerator-runtime/runtime'
// let regeneratorRuntime = require("../../utils/regenerator-runtime/runtime");
import { request } from '../../request/index'
Page({
  beMember: function (e) {
    var app = getApp()
    request({ url: app.globalData.root + 'api/beMember', method: 'POST', data: { money: e.target.dataset.price, id: app.globalData.person.userId, month: e.target.dataset.month }, dataType: 'json' }).then((res) => {
      request({ url: app.globalData.root + 'api/getUser', method: 'GET', dataType: 'json', data: { id: app.globalData.userId } }).then((res) => {
        app.globalData.person = {
          name: res.data[0].userName,
          userId: res.data[0].userId,
          isMember: res.data[0].isMember,
          avatar: res.data[0].avatar,
          deadLine: `${new Date(res.data[0].deadLine).getFullYear()}-${new Date(res.data[0].deadLine).getMonth() + 1}-${new Date(res.data[0].deadLine).getDate()}`,
          money: res.data[0].money,
        }
        this.setData({ person: app.globalData.person })
      })
    })
  },
  predict: function (e) {
    var app = getApp()
    var data = { 
      id: this.data.list[e.currentTarget.dataset.i].id, 
      time: this.data.date + '-' + this.data.time 
    }
    console.log(this.data.date + '-' + this.data.time)
    request({ url: app.globalData.root + 'api/predict', method: 'GET', data: data, dataType: 'json' }).then((res) => {
      request({ url: app.globalData.root + 'api/orderFind', dataType: 'json', method: 'GET', data: { userId: app.globalData.userId } }).then((res) => {
        this.setData({
          list: res.data
        })
      })
    })
  },
  coachComplete: function (e) {
    var app = getApp()
    var data={ 
      coachId: app.globalData.userId, 
      id: this.data.termOrder[e.currentTarget.dataset.i].id 
    }
    request({ url: app.globalData.root + 'api/complete', method: 'GET', data: data, dataType: 'json' }).then((res) => {
      request({ url: app.globalData.root + 'api/orderFind', dataType: 'json', method: 'GET', data: { coachId: app.globalData.userId } }).then((res) => {
        this.setData({
          termOrder: res.data
        })
      })
    })

  },
  userComplete: function (e) {
    var app = getApp()
    var data = { 
      userId: app.globalData.userId, 
      id: this.data.list[e.currentTarget.dataset.i].id 
    }
    request({ url: app.globalData.root + 'api/complete', method: 'GET', data: data, dataType: 'json' }).then((res) => {
      request({ url: app.globalData.root + 'api/orderFind', dataType: 'json', method: 'GET', data: { userId: app.globalData.userId } }).then((res) => {
        this.getAvatar(res.data)
        this.setData({
          list: res.data
        })
      })
    })

  },
  bindDateChange: function (e) {
    this.data.times[e.currentTarget.dataset.i].date = e.detail.value
    this.setData({
      times: [...this.data.times]
    })
  },
  bindTimeChange: function (e) {
    this.data.times[e.currentTarget.dataset.i].time = e.detail.value
    this.setData({
      times: [...this.data.times]
    })
  },
  /**
   * 页面的初始数据
   */
  data: {
    position: 2,
    person: {
      // name: '小雨',
      // avatar: "http://129.28.168.85/img/1.png",
      // isMember: false
    },
    memberlist: [
      299, 399, 499, 599
    ],
    list: [
      // { id: 1, coachId: '贺正', avatar: 'http://129.28.168.85/img/1.png', name: '减脂 | 增肌', PraiseDegree: 100, number: 715, isEmpty: true, price: 300 },
      // { id: 2, coachId: '薪子', avatar: 'http://129.28.168.85/img/1.png', name: '减脂 | 增肌', PraiseDegree: 80, number: 600, isEmpty: true, price: 255 },
    ],
    content: [
      // { time: '4-9 14:22', content: '今天的第一个fdfdasfasdfasfasdfasdfasfasdfasf团队课', name: '小肥星', title: '厚实你的腹肌', number: 23, imgUrl: 'http://129.28.168.85/img/1.png', avatar: 'http://129.28.168.85/img/1.png' },
      // { time: '4-9 14:22', content: '今天的第一个团队课', name: '小肥星', title: '厚实你的腹肌', number: 23, imgUrl: 'http://129.28.168.85/img/1.png', avatar: 'http://129.28.168.85/img/1.png' },
    ],
    termOrder: [
      // { name: '小肥星', userId: '3023704769', time: '4-9 22:30', number: 8 },
      // { name: '大肥星', userId: '3023704769', time: '4-9 22:30', number: 8 },
      // { name: '小瘦星', userId: '3023704769', time: '4-9 22:30', number: 8 },
    ],
    date: '2016-09-01',
    time: '12:01',
    root: '',
    imgs: [],
    times: [
    ]
  },
  getAvatar: function (arr) {
    var app = getApp()

    for (let index = 0; index < arr.length; index++) {
      request({ url: app.globalData.root + 'api/getUser', dataType: 'json', method: 'GET', data: { id: arr[index].coachId } }).then((res) => {
        this.data.imgs[index] = res.data[0].avatar
        this.setData({
          imgs: [...this.data.imgs]
        })
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var app = getApp()
    this.setData({
      person: app.globalData.person,
      position: app.globalData.position,
      root: app.globalData.root,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var app = getApp()
    request({ url: app.globalData.root + 'api/getUser', method: 'GET', dataType: 'json', data: { id: app.globalData.userId } }).then((res) => {
      app.globalData.person = {
        name: res.data[0].userName,
        userId: res.data[0].userId,
        isMember: res.data[0].isMember,
        avatar: res.data[0].avatar,
        deadLine: res.data[0].deadLine,
        money: res.data[0].money,
      }
    })
    if (app.globalData.position === 1) {
      request({ url: app.globalData.root + 'api/orderFind', dataType: 'json', method: 'GET', data: { userId: app.globalData.userId } }).then((res) => {
        this.getAvatar(res.data)
        res.data.forEach((item, index) => {
          this.data.times.push({
            date: '2020-04-01',
            time: '12:01'
          })
        })
        this.setData({
          list: res.data,
          times: [...this.data.times]
        })
      })
      request({ url: app.globalData.root + 'api/teamFind', dataType: 'json', method: 'GET', data: { userId: app.globalData.userId } }).then((res) => {
        res.data.forEach((item, index) => {
          var time = new Date(item.time)
          item.time = `${time.getFullYear()}-${time.getMonth() + 1}-${time.getDate()} ${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}`
        });
        this.setData({
          content: res.data
        })
      })
    } else {
      request({ url: app.globalData.root + 'api/orderFind', dataType: 'json', method: 'GET', data: { coachId: app.globalData.userId } }).then((res) => {

        this.setData({
          termOrder: res.data
        })
      })
      request({ url: app.globalData.root + 'api/teamFind', dataType: 'json', method: 'GET', data: { coachId: app.globalData.userId } }).then((res) => {
        res.data.forEach((item, index) => {
          if (item.time !== '') {
            var time = new Date(item.time)
            item.time = `${time.getFullYear()}-${time.getMonth() + 1}-${time.getDate()} ${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}`
          }
        });
        this.setData({
          content: res.data
        })
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})